//
//  WTPictureMainViewController.m
//  OneDay
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTPictureMainViewController.h"
#import "WTPictureCell.h"
#import "WTRequestManager.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>


@interface WTPictureMainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

// 创建collectionView
@property (nonatomic, strong) UICollectionView * collectionView;

// 数据解析完的数组
@property (nonatomic, strong) NSMutableArray * listArray;

// 请求位置
@property (nonatomic, assign) NSInteger requestStart;

// 等待数据加载完成
@property (nonatomic, strong) MBProgressHUD * hud;

@end

@implementation WTPictureMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _requestStart = 1;
    [self addCollectionView];
    
    
    _listArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
}
#pragma mark -- 创建collectionView
- (void)addCollectionView
{
    // 创建网状结构
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 10 * kRelativelength_Y;
    layout.itemSize = CGSizeMake(kScreenWidth, 280 * kRelativeLength_X);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 - 64 - 30 * kRelativelength_Y) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[WTPictureCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    
#pragma mark -- 明杰刷新
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 1;
        [self requestData];
    }];
    
    _collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _requestStart++;
        [self requestData];
    }];
    
    [self setupHud];
}

#pragma mark --
- (void)setupHud
{
    _hud = [[MBProgressHUD alloc]initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.labelText = @"正在努力加载...";
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}


// 网络请求
- (void)requestData
{
    NSString * url = [NSString stringWithFormat:@"http://api.wap.miercn.com/api/2.0.3/pic_list.php?plat=android&proct=mierapp&versioncode=20150807&apiCode=4&page=%ld",(long)_requestStart];
   [WTRequestManager requestWithUrlString:url parDic:nil method:GET success:^(NSData *data) {
       
       NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       NSArray * imglistArray = dic[@"imglist"];
       
       if (_requestStart == 1) {
           [_listArray removeAllObjects];
       }
       for (NSDictionary * oneDic in imglistArray) {
           WTPictureModel * model = [[WTPictureModel alloc]init];
           [model setValuesForKeysWithDictionary:oneDic];
           [_listArray addObject:model];
       }
       [_collectionView reloadData];
       [_collectionView.mj_header endRefreshing];
       [_collectionView.mj_footer endRefreshing];
       [_hud hide:YES];
   } error:^(NSError *error) {
       
   }];

}



#pragma mark -- dataSource的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WTPictureCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    WTPictureModel * model = _listArray[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark -- 跳转的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WTPictureModel * model = _listArray[indexPath.row];
    self.block(model.ID, model.title);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
